package cn.hd01.web.controller;

import java.math.BigDecimal;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hd01.common.BaseConstant;
import cn.hd01.common.BaseConstant.CustomerType;
import cn.hd01.common.BaseConstant.RebateSummaryType;
import cn.hd01.common.BaseConstant.ServantType;
import cn.hd01.repository.entity.RebateSummary;
import cn.hd01.repository.entity.Servant;
import cn.hd01.service.CustomerService;
import cn.hd01.service.RebateSummaryService;
import cn.hd01.service.ServantService;
import cn.hd01.web.auth.Auth;
import cn.hd01.web.util.WebException;

@Controller
@RequestMapping("/servant")
@Auth
public class ServantController {
	
	@Autowired
	private ServantService servantService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private RebateSummaryService rebateSummaryService;
	
	@RequestMapping(method = RequestMethod.GET)
	public String index(Model m, HttpServletRequest request) {
		String type = request.getParameter("type");
		//老师
		String name = "\u8001\u5E08";
		if(StringUtils.isEmpty(type) || BaseConstant.ServantType.STAFF.equals(type)){
			name = "\u4EBA\u5458";//人员
		} else {
			type = BaseConstant.ServantType.TEACHER;
		}
		m.addAttribute("displayName", name);
		m.addAttribute("type", type);
		return "servant";
	}

	@ResponseBody
	@RequestMapping(path = "/list", method = RequestMethod.GET)
	private Page<Servant> find(Servant s, @PageableDefault Pageable pageable) {
		return servantService.findAll(s, pageable); 
	}

	@ResponseBody
	@RequestMapping(path = "/delete/{id}", method = RequestMethod.GET)
	private void delete(@PathVariable("id") Integer id) {
		servantService.delete(id);
		customerService.updateCustomerServant(id, CustomerType.CUSTOMER);
	}

	@ResponseBody
	@RequestMapping(path = "/save", method = RequestMethod.POST)
	private void save(Servant servant) {

		validateServant(servant);
		
		if(servant.getId() == null){//保存
			
			Date date = new Date();
			servant.setCreateTime(date);
			servant.setUpdateTime(date);
			
			servant = servantService.save(servant);
			
			if(ServantType.TEACHER.equals(servant.getType())){
				RebateSummary rs = new RebateSummary();
				rs.setAllRebate(BigDecimal.ZERO);
				rs.setAllWithdrawRebate(BigDecimal.ZERO);
				rs.setAllWaitWithdrawRebate(BigDecimal.ZERO);
				rs.setCreateTime(date);
				rs.setName(servant.getName());
				rs.setRefId(servant.getId());
				rs.setType(RebateSummaryType.DISTRIBUTOR);
				
				rebateSummaryService.save(rs);
			}
			
		}else {//更新
			servantService.updateServant(servant);
		}
	}
	
	private void validateServant(Servant servant){
		
		if(StringUtils.isEmpty(servant.getName())){
			throw new WebException(HttpStatus.BAD_REQUEST, "名字不能为空");
		}
		
		if(StringUtils.isEmpty(servant.getPhoneNo())){
			throw new WebException(HttpStatus.BAD_REQUEST, "电话号码不能为空");
		}
		
	}
}
